<?php


namespace app\controller;


use app\model\MqttCarPushInfo;
use app\model\ViidDevice;
use think\facade\Log;
use think\facade\Request;

class Faces extends BackendController{
    protected $model = null;
    public function _initialize()
    {
        //parent::_initialize();
        $this->model = new \app\model\Faces();
    }

    public function compare($id){
        $res=$this->model->compare_by_face_id($id);
    }

    /**
     * 以图搜图
     * @param $photo
     * @param string $start_time
     * @param string $end_time
     */
    public function compare_photo($photo,$start_time='',$end_time=''){
        $res=$this->model->compare_photo($photo,$start_time,$end_time);
        $this->success('比较成功',$res);
    }

    public function before_del($lists){
        foreach ($lists as $list) {
            $bg_path=$list['file_path'].$list['id'].'_bg.jpg';
            $face_path=$list['file_path'].$list['id'].'_face.jpg';
            if(file_exists($bg_path)){
                unlink($bg_path);
            }
            if(file_exists($face_path)){
                unlink($face_path);
            }
        }

    }

    /**
     * 非1400推送
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function facein(){
        // S1L S1M 数据有一点差异 ， S5数据完全不一样
        $now_time=date("Y-m-d H:i:s");
        Log::info($now_time.' facein 接口被请求');
        $recive= file_get_contents('php://input');
        // Log::info("face_in_：：：".$recive);
        $get_json=json_decode($recive);
        if($get_json->body->face[0]->confidence<=50)
        {
            //Log::error('facein score 低于 50 返回');
            $this->error('face_bad');
        }
        if($get_json->cmd!='face_snapshot'){
            $this->error('face_request');
        }
        $serialno=$get_json->body->serialno;
        // 日志显示指定摄像头的数据
        if(in_array($serialno,['44876591-414c5ce9'])){
            // Log::info("face_in_指定：：：".$recive);
        }
        $device=ViidDevice::where('ape_id','=',$serialno)->find();
        if(!$device){
            Log::error('no_device');
            $this->error('no_device');
        }
        //Log::info(json_encode($device->toArray()));
        $face['DeviceID']=$device['device_id'];
        // $get_json->body->face[0]->face_id
        // s1M型号没有reco_id
        $http_face_id_timestamp = $get_json->body->timestamp->sec.$get_json->body->timestamp->msec;
        $face['FaceID']=$face['DeviceID'].$http_face_id_timestamp;
        $face['SourceID']=$face['DeviceID'].$http_face_id_timestamp;

        $images[0]['Data']=$get_json->body->picture[0]->data;
        $images[0]['ImageID']=$face['FaceID'].'0';
        $images[1]['Data']=$get_json->body->snapshot->data;
        $images[1]['ImageID']=$face['FaceID'].'1';
        $face['SubImageList']['SubImageInfoObject'][0]=$images[1];
        $face['SubImageList']['SubImageInfoObject'][1]=$images[0];
        $faces['FaceListObject']['FaceObject'][]=$face;
        $content=json_encode($faces);
        //Log::info('content:::'.$content);
        $_face=new \app\model\Faces();
        $_face->saev_face($content);
        /*$job_data = [];
        $job_data["ts"] = time();
        $job_data["bizid"] = uniqid();
        $job_data["params"] =$content;*/
        //$is_pushed=Queue::push(Capture::class,$job_data,'capture_list');
        $this->success();

    }

    public function carin(){
        $recive= file_get_contents('php://input');
        Log::info("车辆推送：：：".$recive);
        $recive=json_decode($recive,true);
        $_car=new MqttCarPushInfo();
        $_car->save_push($recive);
    }

    public function heartbeat(){
        $recive= file_get_contents('php://input');
        Log::info("推送心跳：：：".$recive);
        $User_Identify=Request::header();
        Log::info("推送心跳1：：：".json_encode($User_Identify));
        $this->success();
    }

}